{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea043a68",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "BASE_URL = \"http://0.0.0.0:4008\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca84568",
   "metadata": {},
   "source": [
    "## reranker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "050b258a",
   "metadata": {},
   "outputs": [],
   "source": [
    "queries = [\"What is the capital of China?\", \"Explain gravity\", \"Explain gravity\"]\n",
    "documents = [\n",
    "    \"The capital of China is Beijing.\",\n",
    "    \"The capital of China is Beijing.\",\n",
    "    \"Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c730c917",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.99853515625, 2.390146255493164e-05, 0.99853515625]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = requests.post(\n",
    "    f\"{BASE_URL}/reranker/api\",\n",
    "    json={\"questions\": queries, \"texts\": documents},\n",
    ")\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3db03df",
   "metadata": {},
   "source": [
    "## embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3e0a77e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "queries = [\"What is the capital of China?\", \"Explain gravity\"]\n",
    "documents = [\n",
    "    \"The capital of China is Beijing.\",\n",
    "    \"Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.\",\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "37811bf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1024)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np \n",
    "response = requests.post(\n",
    "    f\"{BASE_URL}/embedding/api\",\n",
    "    json={\"input\": queries,\"is_query\": True},\n",
    ")\n",
    "query_embeddings = response.json()\n",
    "\n",
    "query_embeddings = np.array(query_embeddings)\n",
    "query_embeddings.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9fa11db9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1024)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = requests.post(\n",
    "    f\"{BASE_URL}/embedding/api\",\n",
    "    json={\"input\": documents, \"is_query\": False},\n",
    ")\n",
    "documents_embeddings = response.json()\n",
    "\n",
    "documents_embeddings = np.array(documents_embeddings)\n",
    "documents_embeddings.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7b3a7277",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.76452172, 0.14139068],\n",
       "       [0.13580755, 0.59995794]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_embeddings @ documents_embeddings.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6a72d6d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-5.08422852e-02, -2.91290283e-02, -3.39746475e-05],\n",
       "       [-1.11236572e-02, -3.42407227e-02, -1.11198425e-03]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_embeddings[:, :3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4fd3a6c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.04724121, -0.02067566,  0.00362206],\n",
       "       [-0.05310059, -0.01506042, -0.00127506]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents_embeddings[:, :3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f45227",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "hz_train_vllm2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
